package cn.demo.service0.controller;

import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.demo.service0.User;

@RestController
public class Service02Controller {
 
	/**
	 * 用于测试ribbon 重试机制
	 */
	int count = 0;

	@GetMapping("user/{userId}/{sleepSec}")
	String user(@PathVariable String userId, @PathVariable int sleepSec) {
		try {
			System.out.println("hello:" + userId);
			count++;
			TimeUnit.SECONDS.sleep(sleepSec - count);
			return "hello:" + userId;
		} catch (Exception e) {
			e.printStackTrace();
			return e.getMessage();
		}
	}

	@PostMapping("user")
	String user(@RequestBody User user) {
		System.out.println(user.toString());
		return "service0-2: "+ user.toString();
	}
	
	@GetMapping("say")
	String say(String str) {
		System.out.println(str);
		return "service0-2: say "+str+hi();
	}

	@Value("${foo}")
	String foo;
	@RequestMapping(value = "/hi")
	public String hi(){
		return foo;
	}
}
